package workexp.alg.question;

import org.junit.Test;
import workexp.alg.env.ListNode;

import java.util.List;

public class RemoveNthFromEnd {


    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast,slow,pre;
        fast=head;
        slow=head;
        //三指针？
        pre=head;
        for (int i = 0; i < n-1 ; i++) {
            fast=fast.next;
        }

        while(null!=fast.next){
            fast= fast.next;
            pre=slow;
            slow= slow.next;
        }
        //边界问题错了多次
        if(pre==slow){
            head=slow.next;
        }else{
            pre.next=slow.next;
        }
        return head;
    }

    @Test
    public void test(){
        ListNode head = ListNode.genList(new int[]{1});
        ListNode node = removeNthFromEnd(head,1);
        node.printLinkList();;
    }

}
